<template>
  <div>
    <el-dialog
      title="销售属性"
      :visible.sync="dialogVisible"
      width="750px"
      :close-on-click-modal="false"
      @close="close"
    >
      <el-form
        ref="form"
        v-loading="loading"
        :model="form"
        :rules="rules"
        size="small"
        label-width="100px"
        label-suffix="："
      >
        <div v-if="list.length===0" class="empty">该类目未配置销售属性</div>
        <el-form-item v-for="(item,index) in list" :key="item.id" :label="item.title">
          <el-checkbox-group v-model="form.specList[index].checkIdsArr">
            <el-checkbox
              v-for="attr in item.attrList"
              :key="attr.id"
              :label="attr.id"
            >{{ attr.title }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round :disabled="loading" @click="hide">取 消</el-button>
        <el-button size="small" round :disabled="loading" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { categoryList } from '@/views/wxaMallModule/api/category'
export default {
  data() {
    return {
      // 校验
      rules: {},
      // 配置
      resolve: null,
      reject: null,
      // 状态
      dialogVisible: null,
      loading: false,
      // 数据
      categoryIds: [],
      list: [],
      form: {
        specList: []
      }
    }
  },
  methods: {
    /**
       * 弹框相关
       */
    open({ categoryIds }) {
      this.categoryIds = categoryIds
      this.fetchData()
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {},

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
       * 数据组
       */
    // 获取类目属性
    fetchData() {
      this.loading = true
      const params = {}
      params.categoryIdList = this.categoryIds
      categoryList(params)
        .then(res => {
          // 合并重复类目
          const list = []
          const specObj = {}
          res.data.forEach(item => {
            if (!specObj[item.id]) {
              specObj[item.id] = {
                id: item.id,
                title: item.title,
                allIdsArr: [],
                checkIdsArr: [],
                attrList: []
              }
            }
            item.attrList.forEach(attrItem => {
              if (!specObj[item.id].allIdsArr.includes(attrItem.id)) {
                specObj[item.id].allIdsArr.push(attrItem.id)
                specObj[item.id].attrList.push(attrItem)
              }
            })
          })
          for (const key in specObj) {
            list.push(specObj[key])
          }
          console.log(list)
          this.form.specList = list
          this.list = list
        })
        .catch(err => {})
        .finally(() => {
          this.loading = false
        })
    },

    /**
       * 按钮组
       */
    // 确 定
    submit() {
      const params = JSON.parse(JSON.stringify(this.form))
      params.specList = params.specList.filter(v => {
        v.attrList = v.attrList.filter(attr => {
          return v.checkIdsArr.includes(attr.id)
        })
        return v.checkIdsArr.length > 0
      })
      this.resolve && this.resolve(params.specList)
      this.hide()
    }

    /**
       * 状态组
       */

    /**
       * 方法组
       */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .empty {
    line-height: 100px;
    height: 100px;
    text-align: center;
  }
</style>
